window.onload=()=>{

    
     //---------------------------------获取文档元素--------------------------------------------
  
     var inputTxts = document.getElementsByTagName("input");  // 所有文本框获取
     var inputTxt1 = inputTxts[0];//文本框1
     var inputTxt2 = inputTxts[1];//文本框2
     var inputTxt3 = inputTxts[2];//文本框3，显示结果
 
     var zhengXieBtn = document.querySelectorAll("#zhengXie>button"); //获取正余弦按钮
     
     var historyRecord =document.getElementById("historyRecord"); //获取左侧 textarea并赋值
         historyRecord.innerHTML = "" ; //初始化左侧 textarea为空
 
     var numbers = document.querySelectorAll("#number>button");//获取数字按钮集合
     var strNum = "" ;//初始化数字按钮的叠加
 
     var operates = document.querySelectorAll("#operate>button");//获取加减乘除按钮集合

    clearAll();//初始化--除了textarea都清空，初始化
    var num = 0 ;//textarea行数记录并初始化
    var denghaobiaoji = "" ;//等号标记初始化
    

    
    

    //------------------------------封装函数-----------------------------------------------------------------------------------------
    var shangCiFuhao = "" ; //标记上次使用的符号。
    var oprtSave = 1 ; //标记数字键输入的文本框
    function Fuhao_inputTxt(){  //通过上次符号判断打开的输入框//打开文本框2输入的数字必须在文本框2中
        if(shangCiFuhao == "x^y" || shangCiFuhao == "+" || shangCiFuhao == "-" || shangCiFuhao == "*" ||shangCiFuhao == "/" ||shangCiFuhao == "%" ||shangCiFuhao == "="){
            if(shangCiFuhao == "x^y" || shangCiFuhao == "+" || shangCiFuhao == "-" || shangCiFuhao == "*" ||shangCiFuhao == "/" ||shangCiFuhao == "%" ){
                denghaobiaoji = "" ;
            }else{
                denghaobiaoji = "=" ;
            }
            inputTxt1.style = "width:108px;"
            inputTxt2.style = "display:inline-block;"
            oprtSave = 2 ; //标记数字键输入的文本框2
        }else{
            inputTxt1.style = "width:220px;"
            inputTxt2.style = "display:none;"
            oprtSave = 1 ; //标记数字键输入的文本框1
        }
    }
    function clearInput1(){ //清空文本框1
        inputTxt1.value = "" ;
    }
    function clearInput2(){ //清空文本框2
        inputTxt2.value = "" ;
    }
    function clearInput3(){ //清空文本框3
        inputTxt3.value = "" ;
    }    
    function dataSave(){ //给left textarea获取并赋值
        historyRecord.innerHTML += hRstr() + "num1:" + inputTxt1.value + "\n" + "num2:" + 
                                  inputTxt2.value + "\n" + "num3:" + inputTxt3.value + "\n";
    }
    function hRstr(){ //为dateSave()中hRstr赋值分割
        return "========" + (++num) +"=========\n" ;
    }
    function Denghaobiaoji(){
        if(denghaobiaoji == "="){
            clearInput2();
            if(isNaN(shangCiFuhao)){
                denghaobiaoji = "" ;
                clearAll();
            }
        }
    }
    function splitInput1txt(length){ //分割文本框一的字符串为数组
        var arr = inputTxt1.value.split(",");
        if(length == undefined){
            length = arr.length;
        }else{
            arr.length = length;
        }
        for(var i = 0 ; i < length ; i++ ){
            if(arr[i] == 0 || arr[i] == undefined ){
                arr[i] = 0 ;
            }else{
                arr[i] = Number(arr[i]);
                if(arr[i] == NaN){
                    alert("第" + (i+1) + "个参数输入有误");
                    return [0] ;
                }
            }
        }
        return arr ;
    }
    function minArr(arr){ //取最小值
        var temp = arr[0] ;
        for(var  i = 0  ; i < arr.length ; i++){
            if(temp > arr[i]){
                temp = arr[i];
            }
        } 
        return temp ;
    }
    function maxArr(arr){ //取最大值
        var temp = arr[0] ;
        for(var  i = 0  ; i < arr.length ; i++){
            if(temp < arr[i]){
                temp = arr[i];
            }
        } 
        return temp ;
    }

    //------------------------------------正余弦按钮事件绑定----------------------------------------
    for(var i = 0 ; i < zhengXieBtn.length ;i++){  //正弦余弦按钮赋值事件
        zhengXieBtn[i].index = i ;
        zhengXieBtn[i].onclick = function(){
            shangCiFuhao = this.innerHTML ;//赋值上一次符号
            Fuhao_inputTxt();//通过上次符号判断打开的输入框
            clearInput2(); //清空文本框2
            switch (this.innerHTML){                
                case "sin" :
                    inputTxt3.value = Math.sin(Number(inputTxt1.value));
                    break;
                case "cos" :
                    inputTxt3.value = Math.cos(Number(inputTxt1.value));
                    break;
                case "tan" :
                    inputTxt3.value = Math.tan(Number(inputTxt1.value));
                    break;
                case "x^y" :
                    break;
                case "x^2" :
                    inputTxt3.value = Math.pow(Number(inputTxt1.value),2);
                    break;
                case "x^3" :
                    inputTxt3.value = Math.pow(Number(inputTxt1.value),3);
                    break;
                default:
                    console.log(this.innerHTML);//测试
                    break;
            }
            if(this.innerHTML != "x^y"){
                dataSave();
                strNum = "" ;
            }
        }
    }
    //---------------------------------------数字按钮事件绑定-----------------------------------------------------------
    for(var i = 0 ; i < 11 ; i++){
        numbers[i].index = i ;
        numbers[i].onclick = function(){
            Denghaobiaoji();
            strNum += numbers[this.index].innerHTML ;//数字叠加
            fuZhiSltTxt(strNum);
        }
    }
    function fuZhiSltTxt(text){//判断给哪个文本框赋值,并赋值
        if(oprtSave == 1){
            inputTxt1.value = text ;
        }else if(oprtSave == 2){
            inputTxt2.value = text ;
        }else{
            console.log("错误");
        }
    }
    var btnC = numbers[11]; //获取btnC按钮
    btnC.onclick = clearAll ; //赋值btnC事件
    //除了textarea都清空
    function clearAll(){
        clearInput1();//清空文本
        clearInput2();
        clearInput3();

        shangCiFuhao = "";//上次使用符号清空
        Fuhao_inputTxt();//通过上次符号判断打开的输入框

        strNum = "" ;//赋值文本框赋值标志位空

    }
    //--------------------------------------赋值加减乘除按钮事件绑定--------------------------------------------------------
    for(var i = 0 ; i < operates.length ; i++ ){
        operates[i].index = i ;
        operates[i].onclick = function(){
            if(this.innerHTML != "="){
                shangCiFuhao = this.innerHTML ;//赋值上一次符号
                Fuhao_inputTxt();//通过上次符号判断打开的输入框
            }else{
                denghaobiaoji = "=" ;//等号标记为=
            }
            switch(operates[this.index].innerHTML){
                case "+":
                    // oprtSave_scysf(operates[this.index].innerHTML);
                    strNum = "" ;
                    break;
                case "-":
                    // oprtSave_scysf(operates[this.index].innerHTML);
                    strNum = "" ;
                    
                    break;
                case "*":
                    // oprtSave_scysf(operates[this.index].innerHTML);
                    strNum = "" ;
                    
                    break;
                case "/":
                    // oprtSave_scysf(operates[this.index].innerHTML);
                    strNum = "" ;
                    
                    break;
                case "%":
                    // oprtSave_scysf(operates[this.index].innerHTML);
                    strNum = "" ;
                    
                    break;
                case "=":
                    {   
                        switch (shangCiFuhao) {
                            case "+":
                                inputTxt3.value = Number(inputTxt1.value) +  Number(inputTxt2.value);
                                break;
                            case "-":
                                inputTxt3.value = Number(inputTxt1.value) -  Number(inputTxt2.value);
                                break;
                            case "*":
                                inputTxt3.value = Number(inputTxt1.value) *  Number(inputTxt2.value);
                                break;
                            case "/":
                                inputTxt3.value = Number(inputTxt1.value) /  Number(inputTxt2.value);
                                break;
                            case "%":
                                inputTxt3.value = Number(inputTxt1.value) %  Number(inputTxt2.value);
                                break;
                            case "x^y":
                                inputTxt3.value = Math.pow(Number(inputTxt1.value),Number(inputTxt2.value));
                                break;
                            default:
                                
                                break;
                        }
                        radioShibie(shangCiFuhao);
                        dataSave();
                        if(isNaN(shangCiFuhao)){
                            inputTxt1.value = inputTxt3.value ;
                        }
                    }
                    
                    break;
                defalut:
                    console.log(operates[this.index].innerHTML,"测试");//测试
                    break;
            }
            

        }
    }    

    //---------------------------------------左右按钮获取及赋值事件----------------------------------------------------
    var left = document.getElementById("left");
    var right = document.getElementById("right");
    //获取计算器列表并赋值样式事件
    var li1 = document.querySelector("#li1") ;
    console.log(li1);//测试
    li1.style = "position:absolute;left:0px;"//初始化

    //获取li0并复制初始化
    var li0 = document.querySelector("#li0");
    li0.style = "position:absolute; transform: translate(-150px);" ;//初始化
    var leftBol = true ;//false为消失，true为显示
    left.onclick = function(){
        if(!leftBol){
            li0.style = "position:absolute; transform: translate(-150px);" ;
            li1.style = "position:absolute;left:0px;"
            leftBol = true ;
        }else{
            li0.style = "" ;
            li1.style = "position:absolute;left:153px;"
            leftBol = false ;
        }
    }

    var li2 = document.querySelector("#li2");//获取右边框
    var rightBol = false ;//false为消失，true为显示
    rightcontrol();//初始化右侧边框
    right.onclick = rightcontrol;//右点击绑定时间
    function rightcontrol(){
        if(rightBol){
            closeforRight(); //正余弦不可用
            li2.style = "" ;

            rightBol = false ;
        }else{
            openforRight();//正余弦可用
            li2.style = "position:absolute; transform: translate(153px);" ;
            rightBol = true ;
        }
    }

    //如果右边框显示，那么只有第一个输入框,按键，等于号显示可用，其他不可点击。
    function closeforRight(){
        for(var i = 0 ; i < zhengXieBtn.length ;i++){//正余弦遍历不可用
            zhengXieBtn[i].disabled = "disabled" ;
        }
        for(var i = 0 ; i < operates.length - 1 ; i++ ){//加减乘除遍历不可用
            operates[i].disabled = "disabled" ;
        }
        clearAll();
    }
    function openforRight(){
        for(var i = 0 ; i < zhengXieBtn.length ;i++){//正余弦遍历可用
            zhengXieBtn[i].disabled = "" ;
        }
        for(var i = 0 ; i < operates.length - 1 ; i++ ){//加减乘除遍历可用
            operates[i].disabled = "" ;
        }
        clearAll();
    }
    //-------------------------------------------------右边特殊运算的获取和绑定---------------------------------
    var radios = document.querySelectorAll("#li2>input");//获取radio
    var douhao = document.querySelector("#li2>#douhao");//获取逗号,
    var fuhao = document.querySelector("#li2>#fuhao");//获取负号-
    var arr = [] ;//初始化数组
    for(var i = 0 ; i < radios.length - 1 ; i++ ){//最后一个是‘置空’所以取length - 1 ;
        radios[i] = this.index ;
        radios[i].onchange = function(){
            shangCiFuhao = this.value ;
        }
    }
    radios[radios.length-1].onchange = function(){clearAll();} ;
    function radioShibie(shangCiFuhao){
        switch(shangCiFuhao){
            case "0" ://取随机数
                arr = splitInput1txt(2);
                arr[0] = Number(arr[0]);
                arr[1] = Number(arr[1]);
                var max = arr[0]>arr[1]? arr[0]:arr[1] ;
                var min = arr[0]>arr[1]? arr[1]:arr[0] ;
                inputTxt3.value = Math.floor(Math.random()*(max - min + 1) + min);
                break;
            case "1" ://向上取整
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.ceil(arr[0]);
                break;
            case "2" ://向下取整
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.floor(arr[0]);
                break;
            case "3" ://四舍五入取整
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.round(arr[0]);
                break;
            case "4" ://取最小值
                arr = splitInput1txt();
                inputTxt3.value = minArr(arr);
                break;
            case "5" :
                arr = splitInput1txt();
                inputTxt3.value = maxArr(arr);
                break;
            case "6" :
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.abs(arr[0]);
                break;
            case "7" :
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.asin(arr[0]);
                break;
            case "8" :
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.acos(arr[0]);
                break;
            case "9" :
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.atan(arr[0]);
                break;
            case "10" :
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.log(arr[0]);
                clearInput1();
                break;
            case "11" :
                inputTxt3.value = Math.LN2;
                clearInput1();
                break;
            case "12" :
                inputTxt3.value = Math.LN10;
                clearInput1();
                break;
            case "13" :
                inputTxt3.value = Math.LOG2E;
                clearInput1();
                break;
            case "14" :
                inputTxt3.value = Math.LOG10E;
                clearInput1();
                break;
            case "15" :
                inputTxt3.value = Math.PI;
                clearInput1();
                break;
            case "16" :
                arr = splitInput1txt(1);
                arr[0] = Number(arr[0]);
                inputTxt3.value = Math.sqrt(arr[0]);
                break;
            case "17" ://置空单选框
                clearAll();
                break;
            default:
                console.log("error1");
                break;

        }
    }
    douhao.onclick = function(){//逗号事件绑定
        strNum += this.value ;//数字叠加
        oprtSave = 1 ;
        fuZhiSltTxt(strNum);
    }
    fuhao.onclick = function(){//负号事件绑定
        strNum += this.value ;//数字叠加
        oprtSave = 1 ;
        fuZhiSltTxt(strNum);
    }
    //-------------------键盘接收数字按键-----------------------
    document.onkeypress = function(e){
        var e = e || window.event ;
        var keycode = e.keyCode ;
    }

    















}